<?php

namespace App\Admin\Controllers\LearnWord;

use App\Admin\Actions\Category\DataExport;
use App\Admin\Repositories\LearnWord\ClickLearn;
use App\Models\Data\ClickLearn as ModelsClickLearn;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class LearnWordController extends AdminController
{
    protected $title = '背单词';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ClickLearn(['learn_word']), function (Grid $grid) {
            $grid->model()->has('learn_word');
            $grid->column('id')->sortable();
            $grid->column('content', '内容')->limit(50);
            $grid->column('translation', '翻译')->limit(50);
            $grid->column('phonetic', '音标');
            $grid->column('audio_url', '音频地址')->display(function () {
                return basename($this->audio_url);
            })->limit(10);
            $grid->column('image_url', '图片名称');
            $grid->column('is_download', '是否下载')->limit(50)->display(function ($value) {
                if ($value == 1) {
                    return '<span style="color: #67C23A">是</span>';
                }
                return '<span style="color: #F56C6C">否</span>';
            });
            $grid->column('is_recite', '是否背诵')->limit(50)->display(function ($value) {
                if ($value == 1) {
                    return '<span style="color: #67C23A">是</span>';
                }
                return '<span style="color: #F56C6C">否</span>';
            });
            $grid->column('type', '类型')->display(function ($value) {
                return ModelsClickLearn::$typeMap[$value];
            });
            $grid->tools(new DataExport('导出数据', '/LearnWordExport'));
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableBatchDelete();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(2);
                $filter->equal('content', '内容')->width(2);
                $filter->like('translation', '翻译')->width(2);
                $filter->like('phonetic', '音标')->width(2);
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('learn_word.grade_id', '年级', ClickLearn::categoryList());
            });
        });
    }
}
